package service

import (
	"context"
	"errors"
	"github.com/kuangshp/go-utils/k"
	"github.com/spf13/cast"
	"gorm.io/gorm"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"strings"
)

type _warehouseAreaService struct{}

func (s _warehouseAreaService) CreateApi(ctx context.Context, req model.WarehouseAreaEntity) error {
	_, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(req.EnterpriseID), dao.WarehouseAreaEntity.WarehouseID.Eq(req.WarehouseID), dao.WarehouseAreaEntity.Name.Eq(req.Name)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("名字已经存在,不能重复")
	}

	if err = dao.WarehouseAreaEntity.WithContext(ctx).Create(&model.WarehouseAreaEntity{
		WarehouseID:  req.WarehouseID,  // 关联到warehouse表主键id
		EnterpriseID: req.EnterpriseID, // 关联到enterprise表主键id
		Name:         req.Name,         // 仓库名称
		Code:         req.Code,         // 仓库编码
		Status:       req.Status,       // 状态0表示正常,1表示禁用
		IsDefault:    req.IsDefault,    // 是否为默认仓库,0表示不是，1表示是
		Describe:     req.Describe,     // 仓库名称描述
	}); err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _warehouseAreaService) DeleteByIdApi(ctx context.Context, enterpriseId int64, id int64) error {
	if _, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseAreaEntity.ID.Eq(id)).Delete(); err != nil {
		return err
	}
	return nil
}

func (s _warehouseAreaService) GetListApi(ctx context.Context, enterpriseId int64, warehouseId int64) ([]*model.WarehouseAreaEntity, error) {
	return dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseAreaEntity.Status.Eq(0), dao.WarehouseAreaEntity.WarehouseID.Eq(warehouseId)).Order(dao.WarehouseAreaEntity.ID.Desc()).Find()
}

func (s _warehouseAreaService) GetPageApi(ctx context.Context, req bo.WarehouseAreaPageReqBo) (model []bo.WarehouseAreaPageRespBo, count int64, err error) {
	tx := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(req.EnterpriseId), dao.WarehouseAreaEntity.WarehouseID.Eq(req.WareHouseId))
	if req.Name != "" {
		req.Name = strings.ReplaceAll(req.Name, "%", "\\%")
		req.Name = strings.ReplaceAll(req.Name, "_", "\\%")
		tx = tx.Where(dao.WarehouseAreaEntity.Name.Like("%" + req.Name + "%")).Or(dao.WarehouseAreaEntity.Code.Like("%" + req.Name + "%"))
	}
	if req.Status != "" {
		tx = tx.Where(dao.WarehouseAreaEntity.Status.Eq(cast.ToInt64(req.Status)))
	}
	offset, limit := utils.DbPage(req.PageNumber, req.PageSize)
	count, err = tx.Select(dao.WarehouseAreaEntity.ALL, dao.WarehouseEntity.Name.As("WarehouseName")).
		LeftJoin(dao.WarehouseEntity, dao.WarehouseEntity.ID.EqCol(dao.WarehouseAreaEntity.WarehouseID)).
		Order(dao.WarehouseAreaEntity.ID.Desc()).
		ScanByPage(&model, offset, limit)
	return model, count, err
}

func (s _warehouseAreaService) ModifyStatusByIdApi(ctx context.Context, id int64) error {
	entity, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.ID.Eq(id)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("传递的id错误")
	}
	if _, err = dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.ID.Eq(id)).UpdateSimple(
		dao.WarehouseAreaEntity.Status.Value(int64(k.If(entity.Status == 0, 1, 0))),
	); err != nil {
		return errors.New("修改失败")
	}
	return nil
}

func (s _warehouseAreaService) ModifyByIdApi(ctx context.Context, id int64, enterpriseId int64, req bo.CreateWarehouseBo) error {
	warehouseAreaEntity, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.ID.Eq(id), dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("库区不存在")
	}
	sameNameEntity, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.WarehouseID.Eq(warehouseAreaEntity.WarehouseID), dao.WarehouseAreaEntity.Name.Eq(req.Name)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) && id != sameNameEntity.ID {
		return errors.New("名字已经存在,不能重复")
	}
	if _, err = dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.ID.Eq(id)).UpdateSimple(
		dao.WarehouseAreaEntity.Name.Value(req.Name),         // 仓库名称
		dao.WarehouseAreaEntity.Code.Value(req.Code),         // 仓库编码
		dao.WarehouseAreaEntity.Describe.Value(req.Describe), // 仓库名称描述
	); err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _warehouseAreaService) ModifyDefaultByIdApi(ctx context.Context, enterpriseId int64, id int64) error {
	entity, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.ID.Eq(id)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("设置默认库区失败")
	}
	if _, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseAreaEntity.WarehouseID.Eq(entity.WarehouseID)).UpdateSimple(
		dao.WarehouseAreaEntity.IsDefault.Value(0), // 是否为默认仓库,0表示不是，1表示是
	); err != nil {
		return errors.New("设置默认库区失败")
	}
	if _, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.ID.Eq(id), dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId)).UpdateSimple(
		dao.WarehouseAreaEntity.IsDefault.Value(1), // 是否为默认仓库,0表示不是，1表示是
	); err != nil {
		return errors.New("设置默认库区失败")
	}
	return nil
}

func (s _warehouseAreaService) SetAllDefaultApi(ctx context.Context, enterpriseId int64, warehouseId int64) error {
	if _, err := dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseAreaEntity.WarehouseID.Eq(warehouseId)).UpdateSimple(
		dao.WarehouseAreaEntity.IsDefault.Value(0), // 是否为默认仓库,0表示不是，1表示是
	); err != nil {
		return errors.New("设置其他为非默认库区失败")
	}
	return nil
}

func (s _warehouseAreaService) GetByEnterpriseIdApi(ctx context.Context, enterpriseId int64, warehouseId int64) (*model.WarehouseAreaEntity, error) {
	return dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseAreaEntity.WarehouseID.Eq(warehouseId)).First()
}

func (s _warehouseAreaService) GetCountByWareHouseIdApi(ctx context.Context, enterpriseId int64, warehouseId int64) (int64, error) {
	return dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseAreaEntity.WarehouseID.Eq(warehouseId)).Count()
}

func (s _warehouseAreaService) GetByIdApi(ctx context.Context, enterpriseId int64, warehouseId int64, warehouseAreaId int64) (*model.WarehouseAreaEntity, error) {
	return dao.WarehouseAreaEntity.WithContext(ctx).Where(dao.WarehouseAreaEntity.EnterpriseID.Eq(enterpriseId), dao.WarehouseAreaEntity.WarehouseID.Eq(warehouseId), dao.WarehouseAreaEntity.ID.Eq(warehouseAreaId)).First()
}

var WarehouseAreaService = new(_warehouseAreaService)
